<?php
class Widget_Banner extends WP_Widget
{
  const TPL_FRONT   = 'WidgetBanner.html';
  const TPL_ADMIN   = 'WidgetBannerAdmin.html';
  
  function Widget_Banner()
  {
    $this->WP_Widget(false, $name = 'Banner', array(
      'description' => 'Simple customizable banner'
      ));
  }
  
  function form($i)
  {
    $d = array(
      'url'    => null,
      'img'    => null,
      'alt'    => null,
      'title'  => null,
      'width'  => null,
      'height' => null,
      'target' => null,
    );
    
    $i = wp_parse_args((array) $i, $d);
    $t = Framework::Twig()->loadTemplate(self::TPL_ADMIN);
    
    $v = array(
      'url'    => input_tag($this->get_field_name('url'),    $i['url'],    'style=width:100%;'),
      'img'    => input_tag($this->get_field_name('img'),    $i['img'],    'style=width:100%;'),
      'alt'    => input_tag($this->get_field_name('alt'),    $i['alt'],    'style=width:100%;'),
      'title'  => input_tag($this->get_field_name('title'),  $i['title'],  'style=width:100%;'),
      'width'  => input_tag($this->get_field_name('width'),  $i['width'],  'size=3'),
      'height' => input_tag($this->get_field_name('height'), $i['height'], 'size=3'),
      'target' => select_tag($this->get_field_name('target'), options_for_select(array(0 => '', 1 => '_blank'), $i['target']), 'style=width:100%;'),
    );
    
    $t->display($v);
  }
  
  function update($new_i, $old_i)
  {
    $i = $old_i;
    
    $i['url']    = $new_i['url'];
    $i['img']    = $new_i['img'];
    $i['alt']    = $new_i['alt'];
    $i['title']  = $new_i['title'];
    $i['width']  = intval($new_i['width']);
    $i['height'] = intval($new_i['height']);
    $i['target'] = intval($new_i['target']);
    
    return $i;
  }
  
  function widget($args, $i)
  {
    extract($args);
    
    $i['img'] = Thumbnail::url($i['img'], $i['width'], $i['height']);
    $t = Framework::Twig()->loadTemplate(self::TPL_FRONT);
    echo $before_widget;
    $t->display($i);
    echo $after_widget;
  }
}